package com.example.anote;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHandler extends SQLiteOpenHelper {
	
	// All Static variables
    // Database Version
    private static final int DATABASE_VERSION = 3;
 
    // Database Name
    private static final String DATABASE_NAME = "aNote";
 
    // Contacts table name
    private static final String TABLE_NOTE = "note";
 
    // Contacts Table Columns names
    private static final String KEY_ID = "id";
    private static final String KEY_TITRE = "titre";
    private static final String KEY_DESCRIPTION = "description";
    private static final String KEY_PROGRESS = "progress";
    private static final String KEY_STAT = "stat";

 
    public DatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
 
    // Creating Tables
    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_NOTE_TABLE = "CREATE TABLE " + TABLE_NOTE + "("
                + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT ," 
        		+ KEY_TITRE + " TEXT,"
                + KEY_DESCRIPTION + " TEXT," 
                + KEY_PROGRESS + " INTEGER," 
                + KEY_STAT + " INTEGER" 
        		+ ")";
        db.execSQL(CREATE_NOTE_TABLE);
    }
 
    // Upgrading database
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NOTE);
 
        // Create tables again
        onCreate(db);
	

}
    
  public void addNote(NoteList note)
  {
	  SQLiteDatabase db = this.getWritableDatabase();
	  
	    ContentValues values = new ContentValues();
	    values.put(KEY_TITRE, note.getTitre());
	    values.put(KEY_DESCRIPTION, note.getDescription());
	    values.put(KEY_PROGRESS, note.getProgress());
	    values.put(KEY_STAT, note.getStat());
	 
	    // Inserting Row
	    db.insert(TABLE_NOTE, null, values);
	    db.close(); // Closing database connection 
	  
  }
  
  public void delNote(int id)
  {
	  SQLiteDatabase db = this.getWritableDatabase();
      db.delete(TABLE_NOTE, KEY_ID + " = ?",
              new String[] { ""+id });
      db.close();
	  
  }
  
  public ArrayList<NoteList> getAllNote()
  {
	  ArrayList<NoteList> noteList = new ArrayList<NoteList>();
	  
	  String selectQuery = "SELECT  * FROM " + TABLE_NOTE;
	  
      SQLiteDatabase db = this.getWritableDatabase();
      Cursor cursor = db.rawQuery(selectQuery, null);

      // looping through all rows and adding to list
      if (cursor.moveToFirst()) {
          do {
              NoteList note = new NoteList(cursor.getInt(0),cursor.getString(1),cursor.getString(2),cursor.getInt(3),cursor.getInt(4));

              // Adding contact to list
              noteList.add(note);
          } while (cursor.moveToNext());
      }

      // return contact list
      return noteList;
	  
  }
  
  public int updateNoteStat(int id, int stat)
  {
	  
	  SQLiteDatabase db = this.getWritableDatabase();
	  
      ContentValues values = new ContentValues();
      values.put(KEY_STAT, stat);

      // updating row
      return db.update(TABLE_NOTE, values, KEY_ID + " = ?",
              new String[] { ""+id });
	  
  }
  
  public int updateNote(int id, String Titre, String Descr, int Proc)
  {
	  
 SQLiteDatabase db = this.getWritableDatabase();
	  
      ContentValues values = new ContentValues();
      values.put(KEY_TITRE, Titre);
      values.put(KEY_DESCRIPTION, Descr);
      values.put(KEY_PROGRESS, Proc);

      // updating row
      return db.update(TABLE_NOTE, values, KEY_ID + " = ?",
              new String[] { ""+id });
	  
	  
  }
  
    
    
}
